package com.xiaoma.verbal.web.admin;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoma.tpo.dao.ClsClassDao;
import com.xiaoma.tpo.domain.ClsClass;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.verbal.service.ForecastService;
import com.xiaoma.verbal.service.QuestionService;

@Controller
@RequestMapping("/test/forecast")
public class ForecastManagementAct {
	@Autowired
	private QuestionService questionService;
	@Autowired
	private ForecastService forecastService;
	@Autowired
	private ClsClassDao clsClassDao;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(Integer fid, Integer forecastNum, String timeRange, Integer status, Integer top, Integer page, Integer rows, ModelMap model) {
		// 当前页
		Map<String, Object> result = new HashMap<String, Object>();
		Pager pager = this.forecastService.list(fid, forecastNum, timeRange, status, top, page, rows);
		result.put("rows", pager.getList());
		result.put("total", pager.getTotalCount());
		return result;

	}

	@RequestMapping(value = "/import", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> importData(@RequestParam(value = "excelData", required = false) MultipartFile file, String fileType, HttpServletRequest request, ModelMap model) {
		// 当前页
		Map<String, Object> result = new HashMap<String, Object>();

		InputStream fis = null;
		Integer[] status = null;
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try {
			fis = file.getInputStream();
			int version = 2007;
			if (fileType != null && fileType.length() > 0 && fileType.equals("xls")) {
				version = 2003;
			}
			status = forecastService.importData(version, fis);
		} catch (Exception e) {
			e.printStackTrace();
			// status = 3;
			json.put("msg", e.getMessage());
		}
		result.put("result", status);

		return result;

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Integer update(@RequestParam MultipartFile file,String name,String desc,String type,Integer seqNum,String recommended,String frequency,String[] qnum, Integer fid, Integer forecastNum, Integer status, String timeRange, HttpServletRequest request, ModelMap model) {
		Integer result = 1;
		// 当前页
		try {
			forecastService.update(file, name, desc, type,seqNum, recommended, frequency,qnum, fid, forecastNum, status, timeRange);

		} catch (Exception e) {
			result = 0;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Integer save(@RequestParam MultipartFile file,String name,String desc,String type,Integer seqNum,String recommended,String frequency,String[] qnum, Integer forecastNum, Integer status, String timeRange, HttpServletRequest request, ModelMap model) {
		//只保存课程中的机经
		
		
		Integer result = 1;
		// 当前页
		try {
			forecastService.save(file,name,desc,type,seqNum, recommended, frequency,qnum, forecastNum, status, timeRange);

		} catch (Exception e) {
			result = 0;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

	@RequestMapping(value = "/hasForecastNum", method = RequestMethod.GET)
	@ResponseBody
	public int hasForecastNum(Integer forecastNum, HttpServletRequest request, ModelMap model) {
		Integer result = 1;
		// 当前页
		try {
			return forecastService.hasForecastNum(forecastNum);

		} catch (Exception e) {
			result = 0;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

}
